package com.ruoyi.project.module.ljw;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgHands;
import com.ruoyi.project.domain.YqgInformationRoom;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import com.ruoyi.project.service.IYqgInformationRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 消费详细Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/ljw/qubing")
public class LjwYqgQubingController extends BaseController {
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgInformationRoomService yqgInformationRoomService;


    /**
     * 查询消费详细列表
     */
    //@RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails) throws ParseException {
        //startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
    //@RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails) {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
    //@RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
    //@RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
    //@RequiresPermissions("project:details:edit")
    @Log(title = "取并", businessType = BusinessType.CANCELMERGER, logicalKey = "handBrand")
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
        String handId = yqgConsumptionDetails.getBeforeLeave();
        // 设参数
        // yqgConsumptionDetails.setMergeHands("");
        yqgConsumptionDetails.setHandState("1");
        yqgConsumptionDetails.setMergeStatus("0");
        // 修改消费详细表
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        if (list.size() == 0) {
            yqgConsumptionDetails.setHandBrand(yqgConsumptionDetails.getBeforeLeave());
            yqgConsumptionDetails.setBeforeLeave(null);
            List<YqgConsumptionDetails> yqgCDL = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            if (yqgCDL.size() != 0) {
                // 批量修改
                for (YqgConsumptionDetails consumptionDetails : yqgCDL) {
                    //list.get(i).setDrawTime(drawtime);
                    consumptionDetails.setMergeHands("");
                    consumptionDetails.setMergeStatus("1");
                    consumptionDetails.setMergeBarBillsId(0L);
                    if (consumptionDetails.getBeforeLeave() != null) {
                        consumptionDetails.setRetentionStatus("0");
                    }
                    yqgConsumptionDetailsService.updateYqgConsumptionDetails(consumptionDetails);
                }
            }
        }
        //if (list.size() != 0) {
        else {
            // 批量修改
            for (YqgConsumptionDetails consumptionDetails : list) {
                //list.get(i).setDrawTime(drawtime);
                consumptionDetails.setMergeHands("");
                consumptionDetails.setMergeStatus("1");
                consumptionDetails.setMergeBarBillsId(0L);
            }
            // 批量更新
            for (YqgConsumptionDetails consumptionDetails : list) {
                yqgConsumptionDetailsService.updateYqgConsumptionDetails(consumptionDetails);
            }
        }

        // 手牌表
        YqgHands yqgHands = new YqgHands();
        yqgHands.setHandBrand(handId);
        List<YqgHands> handlist = yqgHandsService.selectYqgHandsList(yqgHands);
        if (handlist.size() != 0) {
            // 更新手牌表
            yqgHands = handlist.get(0);
            yqgHands.setHandState("1");
            yqgHands.setLockedState("1");
            yqgHandsService.updateYqgHands(yqgHands);
        }

        // 吧台账单表
        YqgBarBills yqgBarBills = new YqgBarBills();
        yqgBarBills.setHandBrand(handId);
        // 手牌状态为在台
        yqgBarBills.setHandState("1");
        List<YqgBarBills> billList = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        if (billList.size() != 0) {
            // 更新吧台账单表
            yqgBarBills = billList.get(0);
            yqgBarBills.setMergeStatus("1");
            yqgBarBillsService.updateYqgBarBills(yqgBarBills);
        }

        // 更新房间信息表
        YqgInformationRoom yqgInformationRoom = new YqgInformationRoom();
        yqgInformationRoom.setHandBrand(handId);
        // 手牌状态为在台
        yqgInformationRoom.setHandState("1");
        List<YqgInformationRoom> roomlist = yqgInformationRoomService.selectYqgInformationRoomList(yqgInformationRoom);
        if (roomlist.size() != 0) {
            // 更新房间信息表
            yqgInformationRoom = roomlist.get(0);
            yqgInformationRoom.setHandState("1");
            yqgInformationRoomService.updateYqgInformationRoom(yqgInformationRoom);
        }

        return new AjaxResult(200, "操作成功");
    }

    /**
     * 删除消费详细
     */
    //@RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
}
